#include <iostream>
#include <vector>
#include "string"

class Animal
{
public:
    std::string name;
    Animal(std::string name) : name(name)
    {
        std::cout << "Animal " << name << " is born" << std::endl;
    }

    virtual std::string_view speak() const
    {
        return "???";
    }

    virtual std::string_view cry()
    {
        return "???";
    }
};

class Cat : public Animal
{
public:
    Cat(std::string name) : Animal(name)
    {
        std::cout << "Cat " << name << " is born" << std::endl;
    }

    virtual std::string_view speak() const
    {
        return "meow";
    }

    std::string_view cry()
    {
        return "cry";
    }
};


class A {
    public:
        virtual void print() {
            std::cout << "A" << std::endl;
        }
};

class B : public A {
    public:
        // 在使用override时，说明已经时virtual了，这个时候可以不用加virtual
        // 该方法增加了final说明符，说明该方法不能被重写
        void print() override final {
            std::cout << "B" << std::endl;
        }
};

class C : public B {
    public:
        // 该方法会报错，因为父类B使用了final说明进行限定
        // void print() override {
        //     std::cout << "C" << std::endl;
        // };

        void print1() {
            std::cout << "C" << std::endl;
        }
};

class Base {
    public:
        virtual Base* getThis() {
            return this;
        }

        void printType() {
            std::cout << "Base" << std::endl;
        }
};

class Derived : public Base {
    public:
        // 通常，重写函数的返回类型必须与基类函数的返回类型一致
	    // 但是, 因为 Derived 从 Base 派生, 所以可以返回 Derived*
        Derived* getThis() override {
            return this;
        }

        void printType() {
            std::cout << "Derived" << std::endl;
        }
};

void virtual_ret_ex() {
    Derived d;
    Base* b = &d;
    d.printType(); // 调用 Derived::getThis(), 返回 Derived*, 调用的是 Derived::printType
    b->printType(); // 调用 Derived::getThis(), 返回 Base*, 调用的是 Base::printType
}


class Base2 {
    public:
        virtual Base2* getThis() {
            return this;
        }
        virtual ~Base2() {
            std::cout << "Base2 destructor" << std::endl;
        }
};

class Derived2 : public Base2 {
    private:
        int* m_arr{};
    public:
        Derived2(int size) : m_arr(new int[size]) {
            std::cout << "Derived2 constructor" << std::endl;
        }
        ~Derived2() {
            delete[] m_arr;
            std::cout << "Derived2 destructor" << std::endl;
        }
};

void virtual_destructor_ex() {
    Derived2 d(10);
    Base2* b = &d;
    delete b;
}

// 纯虚函数示例 - 抽象基类
class Shape {
    protected:
        std::string name;
    public:
        Shape(const std::string& name) : name(name) {}
        
        // 纯虚函数 - 计算面积
        virtual double getArea() const = 0;
        
        // 纯虚函数 - 计算周长
        virtual double getPerimeter() const = 0;
        
        // 普通虚函数 - 显示信息
        virtual void display() const {
            std::cout << "形状: " << name << std::endl;
            std::cout << "面积: " << getArea() << std::endl;
            std::cout << "周长: " << getPerimeter() << std::endl;
        }
        
        // 虚析构函数
        virtual ~Shape() = default;
};

// 圆形类 - 实现纯虚函数
class Circle : public Shape {
    private:
        double radius;
    public:
        Circle(double r) : Shape("圆形"), radius(r) {}
        
        // 实现纯虚函数
        double getArea() const override {
            return 3.14159 * radius * radius;
        }
        
        double getPerimeter() const override {
            return 2 * 3.14159 * radius;
        }
        
        void display() const override {
            std::cout << "半径: " << radius << std::endl;
            Shape::display();
        }
};

// 矩形类 - 实现纯虚函数
class Rectangle : public Shape {
    private:
        double width, height;
    public:
        Rectangle(double w, double h) : Shape("矩形"), width(w), height(h) {}
        
        // 实现纯虚函数
        double getArea() const override {
            return width * height;
        }
        
        double getPerimeter() const override {
            return 2 * (width + height);
        }
        
        void display() const override {
            std::cout << "宽度: " << width << ", 高度: " << height << std::endl;
            Shape::display();
        }
};

// 纯虚函数使用示例
void pure_virtual_example() {
    std::cout << "\n=== 纯虚函数示例 ===" << std::endl;
    
    // Shape shape; // 错误！不能实例化抽象类
    
    // 创建具体的形状对象
    Circle circle(5.0);
    Rectangle rect(4.0, 6.0);
    
    // 使用基类指针数组存储不同的形状
    std::vector<Shape*> shapes = {&circle, &rect};
    
    // 通过多态调用纯虚函数
    for (const auto& shape : shapes) {
        shape->display();
        std::cout << "---" << std::endl;
    }
}

// 纯虚函数基类
class PureAnimal {
    public:
        virtual std::string speak() = 0;
        virtual std::string cry() = 0;
};

class PureDog: public PureAnimal {
    public:
        std::string speak() override {
            return "woof";
        };
        std::string cry() override {
            return "woof";
        };
};

void pure_virtual_example2() {
    PureDog dog;
    PureAnimal *ainimal = &dog;
    ainimal->speak();
    ainimal->cry();
}

class IError {
    public:
        virtual void print() = 0;
        virtual void print2() = 0;
        virtual ~IError() = default;
};

class FileError : public IError {
    public:
        void print() override {
            std::cout << "FileError" << std::endl;
        }
        void print2() override {
            std::cout << "FileError2" << std::endl;
        }
};

class NetworkError : public IError {
    public:
        void print() override {
            std::cout << "NetworkError" << std::endl;
        }
        void print2() override {
            std::cout << "NetworkError2" << std::endl;
        }
};

void virtural_learn1() {
    FileError fileError;
    IError *error = &fileError;
    error->print();
    error->print2();
    NetworkError networkError;
    error = &networkError;
    error->print();
    error->print2();
}
void virtural_learn();

